package com.xuecheng.learning.listener;

import com.xuecheng.api.learning.model.dto.CourseRecordDTO;
import com.xuecheng.api.order.model.dto.OrdersDTO;
import com.xuecheng.api.order.model.vo.PayResultModel;
import com.xuecheng.common.util.JsonUtil;
import com.xuecheng.learning.service.CourseRecordService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(consumerGroup = "consumer_group_course_record", topic = "${order.pay.topic}")
public class CourseRecordConsumer implements RocketMQListener<String> {

    @Autowired
    private CourseRecordService courseRecordService;

    /*
     * 业务分析：
     *   1.获得消息并解析
     *   2.将消息传入到业务方法中，并执行消息消费方法的本地事务
     * */
    @Override
    public void onMessage(String jsonStr) {

        //1.获得消息并解析
        PayResultModel resultModel = JsonUtil.jsonToObject(jsonStr, PayResultModel.class);
        //获得订单数据
        OrdersDTO ordersDTO = resultModel.getOrder();
        //构建学习记录数据
        CourseRecordDTO courseRecordDTO = new CourseRecordDTO();
        courseRecordDTO.setUserName(ordersDTO.getUserName());
        courseRecordDTO.setCoursePubId(ordersDTO.getCoursePubId());
        //2.将消息传入到业务方法中，并执行消息消费方法的本地事务
        courseRecordService.createOrModifyRecord(courseRecordDTO);
    }
}
